package com.zx.server.mapper.impl;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


import com.zx.server.mapper.QydjYptMmService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.coc.client.CryptClient;
import com.coc.client.SignClient;
import com.coc.netty.Response;
import com.coc.requestpojo.SM3MacPoJo;
import com.coc.requestpojo.SM4CbcPoJo;

@Slf4j
@Service
public class QydjYptMmServiceImpl  implements QydjYptMmService {

	@Autowired
	private SqlSessionTemplate baseDao;
	@Autowired
	private CryptClient cryptClient;


	/**
	 * 默认使用范围为1-5，具体使用范围请咨询云密对接人员
	 * 不同的密钥号对应密码产品内不同的密钥
	 * 请注意更新和校验时需要使用相同的密钥才能得到相同的结果
	 */
	//SM3密钥号
	public static final int SM3_KEY_NUM = 41;

	// SM4密钥号
	public static final int SM4_KEY_NUM = 41;

	// HMAC_SM3密钥号
	public static final int HMAC_SM3_KEY = 41;


	Map<String, String> randomMap = new HashMap<>();

	/**
	 * 数据完整性验证
	 */
	@Override
	public String SM3HMac(String plainData) {
		log.info("完整性验证参数为："+ plainData);
		//初始化密码机服务
//		CryptClient cryptClient = new CryptClient();
		SM3MacPoJo poJo = new SM3MacPoJo();
		//传入需要进行hmac-sm3的数据
		poJo.setInData(plainData.getBytes());
		//设置密钥号
		poJo.setKeyNum(SM3_KEY_NUM);
		//使用密钥号时，密钥传null
		poJo.setKey(null);
		//执行hmac-sm3
		Response<byte[]> sm3HMac = cryptClient.getSM3HMac(poJo);
		Base64.Encoder encoder = Base64.getEncoder();
		//将结果转码成字符串
		String characterString = encoder.encodeToString(sm3HMac.getmData());
		return characterString;
	}

	/**
	 * 数据加密
	 */
	@Override
	public String SM4CBCEnc(String plainData) {
		try{
			log.info("加密参数为："+ plainData);
			//初始化密码机服务
//	        CryptClient cryptClient = new CryptClient();
			SM4CbcPoJo poJo = new SM4CbcPoJo();
			//传入需要进行加密的数据
			//这里是第一次转码 将String转为byte[]
			poJo.setInData(plainData.getBytes());
			//设置密钥号
			poJo.setKeyNum(SM4_KEY_NUM);
			//使用密钥号时，密钥传null
			poJo.setKey(null);
			//设置IV, IV随机生成
			byte[] SM4_CBC_IV;
			Response<byte[]> ivResponse = cryptClient.genRandom(16);
			//判断接口是否调用成功
			if (ivResponse.getErrCode() == 0) {
				SM4_CBC_IV = ivResponse.getmData();
			}
			else {
				//打印错误码
				log.info("IV 生成失败，错误码：" + ivResponse.getErrCode());
				throw new RuntimeException("IV 生成失败");
			}
			poJo.setIv(SM4_CBC_IV);
			//执行SM4-CBC
			Response<byte[]> sm4CBCEnc = cryptClient.getSM4CBCEnc(poJo);
			//判断接口是否调用成功
			if (sm4CBCEnc.getErrCode() == 0) {
				//拼接IV值到密文中，可以拼接在密文前或者密文后，本demo在密文后
				int length = sm4CBCEnc.getmData().length;
				byte[] cryptData = new byte[length + 16];
				System.arraycopy(sm4CBCEnc.getmData(), 0, cryptData, 0, length);
				System.arraycopy(SM4_CBC_IV, 0, cryptData, length, 16);
				return Base64.getEncoder().encodeToString(cryptData);
			}
			else {
				//打印错误码
				log.info("加密失败，错误码：" + sm4CBCEnc.getErrCode());
				throw new RuntimeException("加密失败");
			}
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException("异常-加密失败: "+e.getMessage());
		}
	}

	/**
	 * 数据解密
	 */
	@Override
	public String SM4CBCDec(String encryptData) {
		try{
			log.info("解密参数为："+ encryptData);
			//初始化密码机服务
			//	  CryptClient cryptClient = new CryptClient();
			SM4CbcPoJo poJo = new SM4CbcPoJo();
			//传入需要进行加密的数据
			//这里是第一次转码 将String转为byte[]
			byte[] bytes = Base64.getDecoder().decode(encryptData);
			byte[] SM4_CBC_IV = new byte[16];
			byte[] cryptData = new byte[bytes.length - 16];
			//获取密文数据
			System.arraycopy(bytes, 0, cryptData, 0, cryptData.length);
			//获取IV
			System.arraycopy(bytes, cryptData.length, SM4_CBC_IV, 0, 16);
			poJo.setInData(cryptData);
			//设置密钥号
			poJo.setKeyNum(SM4_KEY_NUM);
			//使用密钥号时，密钥传null
			poJo.setKey(null);
			//设置IV
			poJo.setIv(SM4_CBC_IV);
			//执行SM4-CBC
			Response<byte[]> sm4CBCDec = cryptClient.getSM4CBCDec(poJo);
			//判断是否调用成功
			if (sm4CBCDec.getErrCode() == 0) {
				//将byte[]密文转成字符串
				return new String(sm4CBCDec.getmData());
			}
			else {
				//打印错误码
				log.info("解密失败，错误码：" + sm4CBCDec.getErrCode());
				throw new RuntimeException("解密失败");
			}
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException("异常-解密失败: "+e.getMessage());
		}
	}


	/**
	 * 获取随机数
	 * 随机数的时效性由服务端进行管理
	 * @param username 用户名 （来自前端）
	 * @return 随机数
	 */
	@Override
	public Map<String,Object> getRandom(String username) {
		Map<String,Object> resMap = new HashMap<>();
		//初始化密码机服务
//        CryptClient cryptClient = new CryptClient();
		//随机数的长度请传入 8 的倍数
		Response<byte[]> res = cryptClient.genRandom(16);
		Base64.Encoder encoder = Base64.getEncoder();
		//将 byte[]转为 String 转码后才是业务逻辑内的随机数
		String random = encoder.encodeToString(res.getmData());
		//和用户进行绑定
//        randomMap.put(username, random);
		//保存随机数
		try {
			Map<String,Object> map = saveYzmOrRandom("1",username,random,"","1");
			log.info("保存随机数返回结果："+ map);
		}catch(Exception e) {
			e.printStackTrace();
			log.info("保存随机数异常："+e.getMessage());
			resMap.put("msg","保存随机数异常");
			return resMap;
		}
		Integer lenth = random.length();
		resMap.put("plainDataLen",lenth);
		resMap.put("plainData",random);
		return resMap;
	}


	/**
	 *对前端签名的结果进行验签
	 *随机数不能从前端传回，需要通过用户标识在缓存中获取
	 *
	 * @param username 用户名 （来着前端）
	 * @param signData 签名结果 （来着前端）
	 * @param cert     数据库中用户绑定的证书 （来着数据库） 这里做测试从前端拿的
	 * @return 是否验签通过
	 */
	@Override
	public boolean rawVerify(String username, String signData, String cert) {
		//初始化签名验签服务器服务
		SignClient signClient = new SignClient();
		//获取随机数的原文,并移除
		//此处示例为随机数只生效一次，具体的时效性可以自行设计
		String plainData = randomMap.remove(username);
		log.info(cert.replaceAll(" ","+"));
		log.info(signData.replaceAll(" ","+"));
		//将前端的签名数据解 Base64
		Base64.Decoder decoder = Base64.getDecoder();
		//这里url传参会把+丢失，换成空格，这里字节替换回来
		byte[] signDataByte = decoder.decode(signData.replaceAll(" ","+"));
		//证书需替换成从数据库中获取用户绑定的证书
		byte[] certData = Base64.getDecoder().decode(cert);
		//进行验签
		Response<Boolean> booleanResponse = signClient.rawVerify(plainData.getBytes(), signDataByte, certData, 3, false);
		boolean flag = booleanResponse.getmData();
		log.info("验签结果："+flag);
		if(flag){
			log.info("逻辑删除随机数");
			Map maps = saveYzmOrRandom("2",username,"","","2");
			log.info("逻辑删除随机数返回：" + maps);
		}
		return flag;
	}


	/**
	 *对前端签名的结果进行验签
	 *随机数不能从前端传回，需要通过用户标识在缓存中获取
	 *
	 * @param username 用户名 （来着前端）
	 * @param signData 签名结果 （来着前端）
	 * @return 是否验签通过
	 */
	public boolean rawVerify2(String username, String signData) {
		//初始化签名验签服务器服务
		SignClient signClient = new SignClient();
		//获取随机数的原文,并移除
		//此处示例为随机数只生效一次，具体的时效性可以自行设计
		//从数据库获取随机数，和对应的证书信息
//		  String plainData = randomMap.remove(username);
		String plainData = "";
		Map<String,Object> dataMap = getYzmOrRandom("2",username);
		try {
			if(dataMap.containsKey("data") && dataMap.containsKey("code")) {
				String code = String.valueOf(dataMap.getOrDefault("code", ""));
				if("200".equals(code)) {
					Map<String,Object>  resMap = (Map<String,Object>) dataMap.getOrDefault("data", new HashMap());
					plainData = String.valueOf(resMap.getOrDefault("random", ""));
				}else {
					log.info("查询随机数为500");
				}
			}else {
				log.info("查询随机数为空");
				return false;
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.info("查询随机数异常：" + e.getMessage());
			return false;
		}
		//查询对应的证书信息
		String cert = "";

		try {
			Map<String,Object> p = new HashMap<String,Object>();
			p.put("account", username);
			List<Map<String,Object>> dataList = this.baseDao.selectList("yptmm.queryZhInfo", p);
			if(dataList != null && !dataList.isEmpty()) {
				Map<String,Object>  resMap = dataList.get(0);
				cert = String.valueOf(resMap.getOrDefault("certinfo", ""));
			}else {
				log.info("查询证书信息为空");
				return false;
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.info("查询对应证书信息异常：" + e.getMessage());
			return false;
		}
		if("".equals(cert) || "".equals(plainData)) {
			log.info("查询证书信息为空或者随机数为空");
			return false;
		}
		log.info(cert.replaceAll(" ","+"));
		log.info(signData.replaceAll(" ","+"));
		//将前端的签名数据解 Base64
		Base64.Decoder decoder = Base64.getDecoder();
		//这里url传参会把+丢失，换成空格，这里字节替换回来
		byte[] signDataByte = decoder.decode(signData.replaceAll(" ","+"));
		//证书需替换成从数据库中获取用户绑定的证书
		byte[] certData = Base64.getDecoder().decode(cert);
		//进行验签
		Response<Boolean> booleanResponse = signClient.rawVerify(plainData.getBytes(), signDataByte, certData, 3, false);
		boolean flag = booleanResponse.getmData();
		log.info("验签结果："+flag);
		return flag;
	}


	/**
	 * 加密账户数据的password和phone 或者用户数据的password
	 *
	 * @param type  1用户信息信息加密  2账户数据加密
	 * @param id 主键id
	 * @param usrname 用户账户
	 * @return
	 */
	public Map<String,Object> SM4CBCEncByIdOrUsername(String type, String id, String usrname){
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> p = new HashMap<String,Object>();
		List<Map<String,Object>> dataList = new ArrayList<Map<String, Object>>();
		Map<String,Object> dataMap = new HashMap<String, Object>();
		int result = 0;
		try {
			if("1".equals(type)) {
				//用户数据加密
				if(!"".equals(id)) {
					p.put("id", id);
					dataList = this.baseDao.selectList("yptmm.queryYhInfo", p);
				}else {
					p.put("usrname", usrname);
					dataList =this.baseDao.selectList("yptmm.queryYhInfo", p);
				}
			}else if("2".equals(type)){
				//账户数据加密
				if(!"".equals(id)) {
					p.put("id", id);
					dataList =this.baseDao.selectList("yptmm.queryZhInfo", p);

				}else {
					p.put("account", usrname);
					dataList =this.baseDao.selectList("yptmm.queryZhInfo", p);
				}
			}

			if(dataList!= null && !dataList.isEmpty()) {
				dataMap = dataList.get(0);
				p.put("id", String.valueOf(dataMap.getOrDefault("id", "")));
			}else {
				log.info("加密结果为空，未查询到对应的数据");
				map.put("code", "200");
				map.put("msg", "加密结果为空，未查询到对应的数据");
				return map;
			}

			//进行数据加密，再修改库中数据
			if("1".equals(type)) {
				String password = String.valueOf(dataMap.getOrDefault("password", ""));
				if(!"".equals(password)) {
					String password_new = SM4CBCEnc(password);
					p.put("password_new", password_new);
					//修改对应数据
					result = this.baseDao.update("yptmm.updateYhInfoById", p);
				}
			}else if("2".equals(type)) {
				String password = String.valueOf(dataMap.getOrDefault("password", ""));
				String phone = String.valueOf(dataMap.getOrDefault("phone", ""));
				String email = String.valueOf(dataMap.getOrDefault("email", ""));
				if(!"".equals(password)) {
					String password_new = SM4CBCEnc(password);
					p.put("password_new", password_new);
				}
				if(!"".equals(phone)) {
					String phone_new = SM4CBCEnc(phone);
					p.put("phone_new", phone_new);
				}
				if(!"".equals(email)) {
					String email_new = SM4CBCEnc(email);
					p.put("email_new", email_new);
				}
				//修改对应数据
				if(p.containsKey("password_new") || p.containsKey("phone_new")) {
					result = this.baseDao.update("yptmm.updateZhInfoById", p);
				}
			}
		}catch(Exception e) {
			map.put("code", "500");
			map.put("msg", "进行数据加密及修改对应数据异常");
			e.printStackTrace();
			log.error("进行数据加密及修改对应数据异常");
			return map;
		}
		map.put("code", "200");
		map.put("msg", "成功");
		map.put("data", result);
		return map;
	}


	/**
	 *    完整性验证，并且保存是否被篡改结果
	 * @param type  1用户 2账户 3机构
	 * @param id
	 * @param ispl 1为批量
	 * @return
	 */
	public Map<String,Object> SM3HMacById(String type, String id, String ispl){
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> p = new HashMap<String,Object>();
		List<Map<String,Object>> dataList = new ArrayList<Map<String, Object>>();
		Map<String,Object> dataMap = new HashMap<String, Object>();
		int result = 0;
		try {
			if("".equals(ispl)) {
				p.put("id", id);
				if("1".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryYhInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						dataMap = dataList.get(0);
						String password = String.valueOf(dataMap.getOrDefault("password_new", ""));
						String utime = String.valueOf(dataMap.getOrDefault("utime", ""));
						String pwdutime = String.valueOf(dataMap.getOrDefault("pwdutime", ""));
						String resultStr = Stream.of(password, utime, pwdutime)
								.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
								.collect(Collectors.joining(""));
						if("".equals(resultStr)) {
							map.put("code", "500");
							map.put("msg", "需要完整性数据为空");
							return map;
						}
						String hmac_sm3 = SM3HMac(resultStr);
						if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
							//修改对应数据
							p.put("hmac_sm3", hmac_sm3);
							p.put("check_flag", "true");
							result = this.baseDao.update("yptmm.updateYhInfoById", p);
						}
					}
				}else if("2".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryZhInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						dataMap = dataList.get(0);
						String username = String.valueOf(dataMap.getOrDefault("username", ""));
						String password = String.valueOf(dataMap.getOrDefault("password_new", ""));
						String phone = String.valueOf(dataMap.getOrDefault("phone", ""));
						String updatetime = String.valueOf(dataMap.getOrDefault("updatetime", ""));
						String updater = String.valueOf(dataMap.getOrDefault("updater", ""));
						String email = String.valueOf(dataMap.getOrDefault("email", ""));
						String resultStr = Stream.of(username, password, phone,updatetime,updater,email)
								.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
								.collect(Collectors.joining(""));
						if("".equals(resultStr)) {
							map.put("code", "500");
							map.put("msg", "需要完整性数据为空");
							return map;
						}
						String hmac_sm3 = SM3HMac(resultStr);
						if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
							//修改对应数据
							p.put("hmac_sm3", hmac_sm3);
							p.put("check_flag", "true");
							result = this.baseDao.update("yptmm.updateZhInfoById", p);
						}
					}
				}else if("3".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryJgInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						dataMap = dataList.get(0);
						String deptname = String.valueOf(dataMap.getOrDefault("deptname", ""));
						String parentid = String.valueOf(dataMap.getOrDefault("parentid", ""));
						String district = String.valueOf(dataMap.getOrDefault("district", ""));
						String updatetime = String.valueOf(dataMap.getOrDefault("updatetime", ""));
						String resultStr = Stream.of(deptname, parentid, district,updatetime)
								.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
								.collect(Collectors.joining(""));
						if("".equals(resultStr)) {
							map.put("code", "500");
							map.put("msg", "需要完整性数据为空");
							return map;
						}
						String hmac_sm3 = SM3HMac(resultStr);
						if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
							//修改对应数据
							p.put("hmac_sm3", hmac_sm3);
							p.put("check_flag", "true");
							result = this.baseDao.update("yptmm.updateJgInfoById", p);
						}
					}
				}else if("4".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryYwInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						dataMap = dataList.get(0);
						String tyshxydm = String.valueOf(dataMap.getOrDefault("tyshxydm", ""));
						String qymc = String.valueOf(dataMap.getOrDefault("qymc", ""));
						String ancheyear = String.valueOf(dataMap.getOrDefault("ancheyear", ""));
						String i_date = String.valueOf(dataMap.getOrDefault("i_date", ""));
						String djjgmc = String.valueOf(dataMap.getOrDefault("djjgmc", ""));
						String djjg = String.valueOf(dataMap.getOrDefault("djjg", ""));
						String qyzt = String.valueOf(dataMap.getOrDefault("qyzt", ""));
						String district = String.valueOf(dataMap.getOrDefault("district", ""));
						String resultStr = Stream.of(tyshxydm, qymc, ancheyear,i_date,djjgmc,djjg,qyzt,district)
								.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
								.collect(Collectors.joining(""));
						if("".equals(resultStr)) {
							map.put("code", "500");
							map.put("msg", "需要完整性数据为空");
							return map;
						}
						String hmac_sm3 = SM3HMac(resultStr);
						if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
							//修改对应数据
							p.put("hmac_sm3", hmac_sm3);
							p.put("check_flag", "true");
							result = this.baseDao.update("yptmm.updateYwInfoById", p);
						}
					}
				}else if("5".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryLogInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						dataMap = dataList.get(0);
						String tablename = String.valueOf(dataMap.getOrDefault("tablename", ""));
						String userid = String.valueOf(dataMap.getOrDefault("userid", ""));
						String username = String.valueOf(dataMap.getOrDefault("username", ""));
						String departid = String.valueOf(dataMap.getOrDefault("departid", ""));
						String type_log = String.valueOf(dataMap.getOrDefault("type", ""));
						String departname = String.valueOf(dataMap.getOrDefault("departname", ""));
						String accepttime = String.valueOf(dataMap.getOrDefault("accepttime", ""));
						String applyid = String.valueOf(dataMap.getOrDefault("applyid", ""));
						String approip = String.valueOf(dataMap.getOrDefault("approip", ""));
						String modobj_name = String.valueOf(dataMap.getOrDefault("modobj_name", ""));
						String resultStr = Stream.of(tablename,userid,username,departid,type_log,departname,accepttime,applyid,approip,modobj_name)
								.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
								.collect(Collectors.joining(""));
						if("".equals(resultStr)) {
							map.put("code", "500");
							map.put("msg", "需要完整性数据为空");
							return map;
						}
						String hmac_sm3 = SM3HMac(resultStr);
						if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
							//修改对应数据
							p.put("hmac_sm3", hmac_sm3);
							p.put("check_flag", "true");
							result = this.baseDao.update("yptmm.updateLogInfoById", p);
						}
					}
				}else if("6".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryJsQxInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						dataMap = dataList.get(0);
						String userid = String.valueOf(dataMap.getOrDefault("userid", ""));
						String roleid = String.valueOf(dataMap.getOrDefault("roleid", ""));
						String resultStr = Stream.of(userid,roleid)
								.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
								.collect(Collectors.joining(""));
						if("".equals(resultStr)) {
							map.put("code", "500");
							map.put("msg", "需要完整性数据为空");
							return map;
						}
						String hmac_sm3 = SM3HMac(resultStr);
						if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
							//修改对应数据
							p.put("hmac_sm3", hmac_sm3);
							p.put("check_flag", "true");
							result = this.baseDao.update("yptmm.updateJsQxInfoById", p);
						}
					}
				}
			}else if("1".equals(ispl)) {
				//批量更新
				List<Map<String,Object>> resultList = new ArrayList<Map<String, Object>>();
				if("1".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryYhInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						for(int i = 0; i < dataList.size(); i++) {
							Map<String,Object> resMap = new HashMap();
							dataMap = dataList.get(i);
							String uuid = String.valueOf(dataMap.getOrDefault("id", ""));
							String password = String.valueOf(dataMap.getOrDefault("password_new", ""));
							String utime = String.valueOf(dataMap.getOrDefault("utime", ""));
							String pwdutime = String.valueOf(dataMap.getOrDefault("pwdutime", ""));
							String resultStr = Stream.of(password, utime, pwdutime)
									.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
									.collect(Collectors.joining(""));
							if("".equals(resultStr)) {
								log.info("需要完整性数据为空对应id为："+ uuid);
								continue;
							}
							String hmac_sm3 = SM3HMac(resultStr);
							if("".equals(hmac_sm3) || "null".equals(hmac_sm3)) {
								log.info("数据签名失败id为："+ uuid);
								continue;
							}else{
								resMap.put("id", uuid);
								resMap.put("hmac_sm3", hmac_sm3);
								resMap.put("check_flag", "true");
								resultList.add(resMap);
							}
						}
						//批量修改数据
						if(resultList !=null && !resultList.isEmpty()) {
							p.put("list", resultList);
							result = this.baseDao.update("yptmm.batchUpdateYhByIds2", p);
						}
					}
				}else if("2".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryZhInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						for(int i = 0; i < dataList.size(); i++) {
							Map<String,Object> resMap = new HashMap();
							dataMap = dataList.get(i);
							String uuid = String.valueOf(dataMap.getOrDefault("id", ""));
							String username = String.valueOf(dataMap.getOrDefault("username", ""));
							String password = String.valueOf(dataMap.getOrDefault("password_new", ""));
							String phone = String.valueOf(dataMap.getOrDefault("phone", ""));
							String updatetime = String.valueOf(dataMap.getOrDefault("updatetime", ""));
							String updater = String.valueOf(dataMap.getOrDefault("updater", ""));
							String email = String.valueOf(dataMap.getOrDefault("email", ""));
							String resultStr = Stream.of(username, password, phone,updatetime,updater,email)
									.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
									.collect(Collectors.joining(""));
							if("".equals(resultStr)) {
								log.info("需要完整性数据为空对应id为："+ uuid);
								continue;
							}
							String hmac_sm3 = SM3HMac(resultStr);
							if("".equals(hmac_sm3) || "null".equals(hmac_sm3)) {
								log.info("数据签名失败id为："+ uuid);
								continue;
							}else{
								resMap.put("id", uuid);
								resMap.put("hmac_sm3", hmac_sm3);
								resMap.put("check_flag", "true");
								resultList.add(resMap);
							}
						}
						//批量修改数据
						if(resultList !=null && !resultList.isEmpty()) {
							p.put("list", resultList);
							result = this.baseDao.update("yptmm.batchUpdateZhByIds2", p);
						}
					}
				}else if("3".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryJgInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						for(int i = 0; i < dataList.size(); i++) {
							Map<String,Object> resMap = new HashMap();
							dataMap = dataList.get(0);
							String uuid = String.valueOf(dataMap.getOrDefault("id", ""));
							String deptname = String.valueOf(dataMap.getOrDefault("deptname", ""));
							String parentid = String.valueOf(dataMap.getOrDefault("parentid", ""));
							String district = String.valueOf(dataMap.getOrDefault("district", ""));
							String updatetime = String.valueOf(dataMap.getOrDefault("updatetime", ""));
							String resultStr = Stream.of(deptname, parentid, district,updatetime)
									.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
									.collect(Collectors.joining(""));
							if("".equals(resultStr)) {
								log.info("需要完整性数据为空对应id为："+ uuid);
								continue;
							}
							String hmac_sm3 = SM3HMac(resultStr);
							if("".equals(hmac_sm3) || "null".equals(hmac_sm3)) {
								log.info("数据签名失败id为："+ uuid);
								continue;
							}else{
								resMap.put("id", uuid);
								resMap.put("hmac_sm3", hmac_sm3);
								resMap.put("check_flag", "true");
								resultList.add(resMap);
							}
						}
						//批量修改数据
						if(resultList !=null && !resultList.isEmpty()) {
							p.put("list", resultList);
							result = this.baseDao.update("yptmm.batchUpdateJgByIds", p);
						}
					}
				}else if("4".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryYwInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						for(int i = 0; i < dataList.size(); i++) {
							Map<String,Object> resMap = new HashMap();
							dataMap = dataList.get(0);
							String uuid = String.valueOf(dataMap.getOrDefault("id", ""));
							String tyshxydm = String.valueOf(dataMap.getOrDefault("tyshxydm", ""));
							String qymc = String.valueOf(dataMap.getOrDefault("qymc", ""));
							String ancheyear = String.valueOf(dataMap.getOrDefault("ancheyear", ""));
							String i_date = String.valueOf(dataMap.getOrDefault("i_date", ""));
							String djjgmc = String.valueOf(dataMap.getOrDefault("djjgmc", ""));
							String djjg = String.valueOf(dataMap.getOrDefault("djjg", ""));
							String qyzt = String.valueOf(dataMap.getOrDefault("qyzt", ""));
							String district = String.valueOf(dataMap.getOrDefault("district", ""));
							String resultStr = Stream.of(tyshxydm, qymc, ancheyear,i_date,djjgmc,djjg,qyzt,district)
									.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
									.collect(Collectors.joining(""));
							if("".equals(resultStr)) {
								log.info("需要完整性数据为空对应id为："+ uuid);
								continue;
							}
							String hmac_sm3 = SM3HMac(resultStr);
							if("".equals(hmac_sm3) || "null".equals(hmac_sm3)) {
								log.info("数据签名失败id为："+ uuid);
								continue;
							}else{
								resMap.put("id", uuid);
								resMap.put("hmac_sm3", hmac_sm3);
								resMap.put("check_flag", "true");
								resultList.add(resMap);
							}
						}
						//批量修改数据
						if(resultList !=null && !resultList.isEmpty()) {
							p.put("list", resultList);
							result = this.baseDao.update("yptmm.updateYwInfoByIds", p);
						}
					}
				}else if("5".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryLogInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						for(int i = 0; i < dataList.size(); i++) {
							Map<String,Object> resMap = new HashMap();
							dataMap = dataList.get(0);
							String uuid = String.valueOf(dataMap.getOrDefault("id", ""));
							String tablename = String.valueOf(dataMap.getOrDefault("tablename", ""));
							String userid = String.valueOf(dataMap.getOrDefault("userid", ""));
							String username = String.valueOf(dataMap.getOrDefault("username", ""));
							String departid = String.valueOf(dataMap.getOrDefault("departid", ""));
							String type_log = String.valueOf(dataMap.getOrDefault("type", ""));
							String departname = String.valueOf(dataMap.getOrDefault("departname", ""));
							String accepttime = String.valueOf(dataMap.getOrDefault("accepttime", ""));
							String applyid = String.valueOf(dataMap.getOrDefault("applyid", ""));
							String approip = String.valueOf(dataMap.getOrDefault("approip", ""));
							String modobj_name = String.valueOf(dataMap.getOrDefault("modobj_name", ""));
							String resultStr = Stream.of(tablename,userid,username,departid,type_log,departname,accepttime,applyid,approip,modobj_name)
									.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
									.collect(Collectors.joining(""));
							if("".equals(resultStr)) {
								log.info("需要完整性数据为空对应id为："+ uuid);
								continue;
							}
							String hmac_sm3 = SM3HMac(resultStr);
							if("".equals(hmac_sm3) || "null".equals(hmac_sm3)) {
								log.info("数据签名失败id为："+ uuid);
								continue;
							}else{
								resMap.put("id", uuid);
								resMap.put("hmac_sm3", hmac_sm3);
								resMap.put("check_flag", "true");
								resultList.add(resMap);
							}
						}
						//批量修改数据
						if(resultList !=null && !resultList.isEmpty()) {
							p.put("list", resultList);
							result = this.baseDao.update("yptmm.updateLogInfoByIds", p);
						}
					}
				}else if("6".equals(type)) {
					dataList = this.baseDao.selectList("yptmm.queryJsQxInfo", p);
					if(dataList != null && !dataList.isEmpty()) {
						for(int i = 0; i < dataList.size(); i++) {
							Map<String,Object> resMap = new HashMap();
							dataMap = dataList.get(0);
							String uuid = String.valueOf(dataMap.getOrDefault("id", ""));
							String userid = String.valueOf(dataMap.getOrDefault("userid", ""));
							String roleid = String.valueOf(dataMap.getOrDefault("roleid", ""));
							String resultStr = Stream.of(userid,roleid)
									.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
									.collect(Collectors.joining(""));
							if("".equals(resultStr)) {
								log.info("需要完整性数据为空对应id为："+ uuid);
								continue;
							}
							String hmac_sm3 = SM3HMac(resultStr);
							if("".equals(hmac_sm3) || "null".equals(hmac_sm3)) {
								log.info("数据签名失败id为："+ uuid);
								continue;
							}else{
								resMap.put("id", uuid);
								resMap.put("hmac_sm3", hmac_sm3);
								resMap.put("check_flag", "true");
								resultList.add(resMap);
							}
						}
						//批量修改数据
						if(resultList !=null && !resultList.isEmpty()) {
							p.put("list", resultList);
							result = this.baseDao.update("yptmm.updateJsQxInfoByIds", p);
						}
					}
				}
			}
		}catch(Exception e){
			map.put("code", "500");
			map.put("msg", "进行数据完整性验证及修改对应数据异常");
			e.printStackTrace();
			log.error("进行数据完整性验证及修改对应数据异常");
		}
		map.put("code", "200");
		map.put("msg", "成功");
		map.put("data", result);
		return map;
	}



	/**
	 *  批量加密
	 * @param type  1用户 2账户
	 * @param id 主键id
	 * @return
	 */
	public Map<String,Object> SM4CBCEncById(String type, String id ){
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> p = new HashMap<String,Object>();
		List<Map<String,Object>> dataList = new ArrayList<Map<String, Object>>();
		Map<String,Object> dataMap = new HashMap<String, Object>();
		List<Map<String,Object>> resultList = new ArrayList<Map<String, Object>>();
		int result = 0;
		try {
			if("1".equals(type)) {
				dataList = this.baseDao.selectList("yptmm.queryYhInfo", p);
				if(dataList != null && !dataList.isEmpty()) {
					for(int i = 0; i < dataList.size(); i++) {
						Map<String,Object> resMap = new HashMap();
						dataMap = dataList.get(i);
						String uuid = String.valueOf(dataMap.getOrDefault("id", ""));
						String password = String.valueOf(dataMap.getOrDefault("password", ""));
						if("".equals(password) || "null".equals(password) ) {
							log.info("用户数据密码为空对应id为："+ uuid);
							continue;
						}
						String password_new = SM4CBCEnc(password);
						if("".equals(password_new) || "null".equals(password_new)) {
							log.info("数据加密失败id为："+ uuid);
							continue;
						}else{
							resMap.put("id", uuid);
							resMap.put("password", password_new);
							resultList.add(resMap);
						}
					}
					//批量修改数据
					if(resultList !=null && !resultList.isEmpty()) {
						p.put("list", resultList);
						result = this.baseDao.update("yptmm.batchUpdateYhByIds", p);
					}
				}
			}else if("2".equals(type)) {
				dataList = this.baseDao.selectList("yptmm.queryZhInfo", p);
				if(dataList != null && !dataList.isEmpty()) {
					for(int i = 0; i < dataList.size(); i++) {
						Map<String,Object> resMap = new HashMap();
						dataMap = dataList.get(i);
						String uuid = String.valueOf(dataMap.getOrDefault("id", ""));
						String password = String.valueOf(dataMap.getOrDefault("password", ""));
						String phone = String.valueOf(dataMap.getOrDefault("phone", ""));
						String email = String.valueOf(dataMap.getOrDefault("email", ""));
						String password_new = "";
						String phone_new= "";
						String email_new= "";
						if("".equals(password) || "null".equals(password) ) {
							log.info("用户数据密码为空对应id为："+ uuid);
						}else {
							password_new = SM4CBCEnc(password);
						}
						if("".equals(phone) || "null".equals(phone) ) {
							log.info("用户数据电话为空对应id为："+ uuid);
						}else {
							phone_new = SM4CBCEnc(phone);
						}
						if("".equals(email) || "null".equals(email) ) {
							log.info("用户数据电话为空对应id为："+ uuid);
						}else {
							email_new = SM4CBCEnc(email);
						}
						if("".equals(password_new) && "".equals(phone_new) && "".equals(email_new)) {
							log.info("用户数据电话为空,,密码为空,邮箱");
							continue;
						}
						resMap.put("id", uuid);
						resMap.put("password", password_new);
						resMap.put("phone", phone_new);
						resMap.put("email", email_new);
						resultList.add(resMap);
					}
				}
				//批量修改数据
				if(resultList !=null && !resultList.isEmpty()) {
					p.put("list", resultList);
					result = this.baseDao.update("yptmm.batchUpdateZhByIds", p);
				}
			}
		}catch(Exception e) {
			map.put("code", "500");
			map.put("msg", "进行数据加密及修改对应数据异常");
			e.printStackTrace();
			log.error("进行数据加密及修改对应数据异常");
		}
		map.put("code", "200");
		map.put("msg", "成功");
		map.put("data", result);
		return map;
	}


	/**
	 * 保存随机数，验证码
	 * @param type 1新增 2删除
	 * @param username
	 * @param random
	 * @param yzm
	 * @return
	 */
	public Map<String,Object> saveYzmOrRandom(String type, String username, String random, String yzm, String operate){
		Map<String,Object> map = new HashMap<String,Object>();
		int result = 0;
		try {
			//查询账户信息
			Map<String,Object> p = new HashMap<String,Object>();
			Map<String,Object> dataMap = new HashMap<String,Object>();
			Map<String,Object> resMap = new HashMap<String,Object>();
			p.put("account", username);
			List<Map<String,Object>> dataList = this.baseDao.selectList("yptmm.queryZhInfo", p);
			if(dataList != null && !dataList.isEmpty()) {
				dataMap = dataList.get(0);
			}else {
				map.put("code", "500");
				map.put("msg", "没有查询到对应的账户信息");
				return map;
			}
			String userid = String.valueOf(dataMap.getOrDefault("id", ""));
			if("1".equals(type)) {
				if("".equals(random)) {
					resMap.put("yzm", yzm);
					resMap.put("type", "1");  //1验证码
				}else {
					resMap.put("random", random);
					resMap.put("type", "2");  //2随机数
				}
				resMap.put("userid", userid);
				resMap.put("username", username);
				resMap.put("id",  UUID.randomUUID().toString().replace("-", ""));
				result = this.baseDao.insert("yptmm.insertYzmRandom", resMap);
			}else if("2".equals(type)) {
				Map<String,Object> dataMaps = new HashMap<String,Object>();
				p.put("userid", userid);
				if("1".equals(operate)) {
					p.put("type", "1");  //1验证码
				}else {
					p.put("type", "2");  //2随机数
				}
				List<Map<String,Object>> dataLists = this.baseDao.selectList("yptmm.queryYzmRandomByUserid", p);
				if(dataLists != null && !dataLists.isEmpty()) {
					dataMaps = dataLists.get(0);
				}else {
					map.put("code", "500");
					map.put("msg", "没有逻辑删除的数据");
					return map;
				}
				String id = String.valueOf(dataMaps.getOrDefault("id", ""));
				Map<String,Object> res = new HashMap<String,Object>();
				res.put("id", id);
				result = this.baseDao.update("yptmm.updateYzmRandomById",res);
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.error("操作随机数异常 ： " + e.getMessage());
			map.put("code", "500");
			map.put("msg", "操作随机数异常");
			return map;
		}
		map.put("code", "200");
		map.put("msg", "成功");
		map.put("data", result);
		return map;
	}


	/**
	 * 保存对应证书数据信息
	 * @param username
	 * @param certinfo
	 * @return
	 */
	public Map<String,Object> saveCertinfo(String username, String certinfo){
		Map<String,Object> map = new HashMap<String,Object>();
		int result = 0;
		try {
			//查询账户信息
			Map<String,Object> p = new HashMap<String,Object>();
			Map<String,Object> dataMap = new HashMap<String,Object>();
			Map<String,Object> resMap = new HashMap<String,Object>();
			p.put("account", username);
			List<Map<String,Object>> dataList = this.baseDao.selectList("yptmm.queryZhInfo", p);
			if(dataList != null && !dataList.isEmpty()) {
				dataMap = dataList.get(0);
			}else {
				map.put("code", "500");
				map.put("msg", "没有查询到对应的账户信息");
				return map;
			}
			String id = String.valueOf(dataMap.getOrDefault("id", ""));
			resMap.put("id", id);
			resMap.put("certinfo", certinfo);
			result = this.baseDao.update("yptmm.updateZhInfoById",resMap);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("操作证书信息异常 ： " + e.getMessage());
			map.put("code", "500");
			map.put("msg", "操作证书信息异常");
			return map;
		}
		map.put("code", "200");
		map.put("msg", "成功");
		map.put("data", result);
		return map;
	}


	/**
	 * 查询对应验证码或者随机数
	 * @param username
	 * @return
	 */
	public Map<String,Object> getYzmOrRandom(String operate, String username){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> dataList = new ArrayList();
		try {
			Map<String,Object> p = new HashMap<String,Object>();
			Map<String,Object> dataMap = new HashMap<String,Object>();
			p.put("account", username);
			List<Map<String,Object>> dataLists = this.baseDao.selectList("yptmm.queryZhInfo", p);
			if(dataLists != null && !dataLists.isEmpty()) {
				dataMap = dataLists.get(0);
			}else {
				map.put("code", "500");
				map.put("msg", "没有查询到对应的账户信息");
				return map;
			}
			String userid = String.valueOf(dataMap.getOrDefault("id", ""));
			Map<String,Object> dataMaps = new HashMap<String,Object>();
			p.put("userid", userid);
			if("1".equals(operate)) {
				p.put("type", "1");  //1验证码
			}else {
				p.put("type", "2");  //2随机数
			}
			dataList = this.baseDao.selectList("yptmm.queryYzmRandomByUserid", p);
			if(dataList == null || dataList.isEmpty()) {
				map.put("code", "500");
				map.put("msg", "没有查询到对应的验证码或者随机数信息");
				return map;
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.error("查询验证码或者随机数为异常 ： " + e.getMessage());
			map.put("code", "500");
			map.put("msg", "查询验证码或者随机数为异常");
			return map;
		}
		map.put("code", "200");
		map.put("msg", "成功");
		map.put("data", dataList.get(0));
		return map;
	}

	/**
	 * 判断账户是否需要发送验证码
	 */
	public Map<String, Object> getSfYzmZh(String username) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			Map<String,Object> p = new HashMap<String,Object>();
			p.put("account", username);
			List<Map<String,Object>> dataList = this.baseDao.selectList("yptmm.queryZhInfo", p);
			if(dataList != null && !dataList.isEmpty()) {
				map.put("code", "200");
				map.put("data", dataList.get(0).getOrDefault("bs",""));
				return map;
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.error("账户是否需要发送验证码或ukey异常 ： " + e.getMessage());
			map.put("code", "500");
			map.put("msg", "账户是否需要发送验证码或ukey异常");
			return map;
		}
		map.put("code", "200");
		map.put("data", "");
		return map;
	}


	/**
	 * 校验验证码是否正确
	 */
	public Map<String, Object> verifyYzm(String yzm, String username) {
		Map<String,Object> map = new HashMap<String,Object>();
		String resYzm = "";
		try {
			Map<String,Object> dataMap = getYzmOrRandom("1",username);
			if(dataMap.containsKey("data") && dataMap.containsKey("code")) {
				String code = String.valueOf(dataMap.getOrDefault("code", ""));
				if("200".equals(code)) {
					Map<String,Object>  resMap = (Map<String,Object>) dataMap.getOrDefault("data", new HashMap());
					resYzm = String.valueOf(resMap.getOrDefault("yzm", ""));
				}else {
					log.info("查询验证码code为500");
					map.put("code", "500");
					map.put("data", "查询验证码code为500");
					return map;
				}
			}else {
				log.info("查询验证数为空");
				map.put("code", "500");
				map.put("data", "查询验证码为空");
				return map;
			}
			if(!yzm.equals(resYzm)) {
				map.put("code", "200");
				map.put("data", false);
				return map;
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.error("查询验证码异常 ： " + e.getMessage());
			map.put("code", "500");
			map.put("data", "查询验证码异常");
			return map;
		}
		map.put("code", "200");
		map.put("data", true);
		return map;
	}

	/**
	 * 检验数据的完整性，并修改状态
	 * @param id
	 * @param type 1 用户 2 账户 3机构 4.业务数据 5.日志数据
	 * @return
	 */
	@Override
	public Map<String, Object> checkSM3HMa(String id, String type) {
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> p = new HashMap<>();
		List<Map<String,Object>> dataList = new ArrayList();
		Map<String, Object> dataMap = new HashMap<>();
		int result = 0;
		try{
			p.put("id", id);
			if("1".equals(type)) {
				dataList = this.baseDao.selectList("yptmm.queryYhInfo", p);
				if(dataList != null && !dataList.isEmpty()) {
					dataMap = dataList.get(0);
					String password = String.valueOf(dataMap.getOrDefault("password_new", ""));
					String utime = String.valueOf(dataMap.getOrDefault("utime", ""));
					String pwdutime = String.valueOf(dataMap.getOrDefault("pwdutime", ""));
					String hmac_sm3_old = String.valueOf(dataMap.getOrDefault("hmac_sm3", ""));
					if("".equals(hmac_sm3_old)){
						map.put("code", "500");
						map.put("msg", "数据之前未做完整性");
						return map;
					}
					String resultStr = Stream.of(password, utime, pwdutime)
							.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
							.collect(Collectors.joining(""));
					if("".equals(resultStr)) {
						map.put("code", "500");
						map.put("msg", "需要完整性数据为空");
						return map;
					}
					String hmac_sm3 = SM3HMac(resultStr);
					if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
						if(hmac_sm3.equals(hmac_sm3_old)) {
							//数据一致不做操作
							map.put("code", "200");
							map.put("msg", "完整性验证成功");
							map.put("data", hmac_sm3);
							return map;
						}
						p.put("check_flag", "false");
						result = this.baseDao.update("yptmm.updateYhInfoById", p);
						map.put("code", "200");
						map.put("result", result);
						map.put("data", hmac_sm3);
						map.put("msg", "数据已被篡改");
					}
				}
			}else if("2".equals(type)) {
				dataList = this.baseDao.selectList("yptmm.queryZhInfo", p);
				if(dataList != null && !dataList.isEmpty()) {
					dataMap = dataList.get(0);
					String username = String.valueOf(dataMap.getOrDefault("username", ""));
					String password = String.valueOf(dataMap.getOrDefault("password_new", ""));
					String phone = String.valueOf(dataMap.getOrDefault("phone", ""));
					String updatetime = String.valueOf(dataMap.getOrDefault("updatetime", ""));
					String updater = String.valueOf(dataMap.getOrDefault("updater", ""));
					String email = String.valueOf(dataMap.getOrDefault("email", ""));
					String hmac_sm3_old = String.valueOf(dataMap.getOrDefault("hmac_sm3", ""));
					if("".equals(hmac_sm3_old)){
						map.put("code", "500");
						map.put("msg", "数据之前未做完整性");
						return map;
					}
					String resultStr = Stream.of(username, password, phone,updatetime,updater,email)
							.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
							.collect(Collectors.joining(""));
					if("".equals(resultStr)) {
						map.put("code", "500");
						map.put("msg", "需要完整性数据为空");
						return map;
					}
					String hmac_sm3 = SM3HMac(resultStr);
					if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
						if(hmac_sm3.equals(hmac_sm3_old)) {
							//数据一致不做操作
							map.put("code", "200");
							map.put("msg", "完整性验证成功");
							map.put("data", hmac_sm3);
							return map;
						}
						p.put("check_flag", "false");
						result = this.baseDao.update("yptmm.updateZhInfoById", p);
						map.put("code", "200");
						map.put("result", result);
						map.put("data", hmac_sm3);
						map.put("msg", "数据已被篡改");
					}
				}
			}else if("3".equals(type)) {
				dataList = this.baseDao.selectList("yptmm.queryJgInfo", p);
				if(dataList != null && !dataList.isEmpty()) {
					dataMap = dataList.get(0);
					String deptname = String.valueOf(dataMap.getOrDefault("deptname", ""));
					String parentid = String.valueOf(dataMap.getOrDefault("parentid", ""));
					String district = String.valueOf(dataMap.getOrDefault("district", ""));
					String updatetime = String.valueOf(dataMap.getOrDefault("updatetime", ""));
					String hmac_sm3_old = String.valueOf(dataMap.getOrDefault("hmac_sm3", ""));
					if("".equals(hmac_sm3_old)){
						map.put("code", "500");
						map.put("msg", "数据之前未做完整性");
						return map;
					}
					String resultStr = Stream.of(deptname, parentid, district,updatetime)
							.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
							.collect(Collectors.joining(""));
					if("".equals(resultStr)) {
						map.put("code", "500");
						map.put("msg", "需要完整性数据为空");
						return map;
					}
					String hmac_sm3 = SM3HMac(resultStr);
					if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
						if(hmac_sm3.equals(hmac_sm3_old)) {
							//数据一致不做操作
							map.put("code", "200");
							map.put("msg", "完整性验证成功");
							map.put("data", hmac_sm3);
							return map;
						}
						p.put("check_flag", "false");
						result = this.baseDao.update("yptmm.batchUpdateJgById", p);
						map.put("code", "200");
						map.put("result",result);
						map.put("data", hmac_sm3);
						map.put("msg", "数据已被篡改");
					}
				}
			}else if("4".equals(type)) {
				dataList = this.baseDao.selectList("yptmm.queryYwInfo", p);
				if(dataList != null && !dataList.isEmpty()) {
					dataMap = dataList.get(0);
					String tyshxydm = String.valueOf(dataMap.getOrDefault("tyshxydm", ""));
					String qymc = String.valueOf(dataMap.getOrDefault("qymc", ""));
					String ancheyear = String.valueOf(dataMap.getOrDefault("ancheyear", ""));
					String i_date = String.valueOf(dataMap.getOrDefault("i_date", ""));
					String djjgmc = String.valueOf(dataMap.getOrDefault("djjgmc", ""));
					String djjg = String.valueOf(dataMap.getOrDefault("djjg", ""));
					String qyzt = String.valueOf(dataMap.getOrDefault("qyzt", ""));
					String district = String.valueOf(dataMap.getOrDefault("district", ""));
					String hmac_sm3_old = String.valueOf(dataMap.getOrDefault("hmac_sm3", ""));
					if("".equals(hmac_sm3_old)){
						map.put("code", "500");
						map.put("msg", "数据之前未做完整性");
						return map;
					}
					String resultStr = Stream.of(tyshxydm, qymc, ancheyear,i_date,djjgmc,djjg,qyzt,district)
							.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
							.collect(Collectors.joining(""));
					if("".equals(resultStr)) {
						map.put("code", "500");
						map.put("msg", "需要完整性数据为空");
						return map;
					}
					String hmac_sm3 = SM3HMac(resultStr);
					if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
						if(hmac_sm3.equals(hmac_sm3_old)) {
							//数据一致不做操作
							map.put("code", "200");
							map.put("msg", "完整性验证成功");
							map.put("data", hmac_sm3);
							return map;
						}
						p.put("check_flag", "false");
						result = this.baseDao.update("yptmm.updateYwInfoById", p);
						map.put("code", "200");
						map.put("result", result);
						map.put("data", hmac_sm3);
						map.put("msg", "数据已被篡改");
					}
				}
			}else if("5".equals(type)) {
				dataList = this.baseDao.selectList("yptmm.queryLogInfo", p);
				if(dataList != null && !dataList.isEmpty()) {
					dataMap = dataList.get(0);
					String tablename = String.valueOf(dataMap.getOrDefault("tablename", ""));
					String userid = String.valueOf(dataMap.getOrDefault("userid", ""));
					String username = String.valueOf(dataMap.getOrDefault("username", ""));
					String departid = String.valueOf(dataMap.getOrDefault("departid", ""));
					String type_log = String.valueOf(dataMap.getOrDefault("type", ""));
					String departname = String.valueOf(dataMap.getOrDefault("departname", ""));
					String accepttime = String.valueOf(dataMap.getOrDefault("accepttime", ""));
					String applyid = String.valueOf(dataMap.getOrDefault("applyid", ""));
					String approip = String.valueOf(dataMap.getOrDefault("approip", ""));
					String modobj_name = String.valueOf(dataMap.getOrDefault("modobj_name", ""));
					String hmac_sm3_old = String.valueOf(dataMap.getOrDefault("hmac_sm3", ""));
					if("".equals(hmac_sm3_old)){
						map.put("code", "500");
						map.put("msg", "数据之前未做完整性");
						return map;
					}
					String resultStr = Stream.of(tablename,userid,username,departid,type_log,departname,accepttime,applyid,approip,modobj_name)
							.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
							.collect(Collectors.joining(""));
					if("".equals(resultStr)) {
						map.put("code", "500");
						map.put("msg", "需要完整性数据为空");
						return map;
					}
					String hmac_sm3 = SM3HMac(resultStr);
					if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
						if(hmac_sm3.equals(hmac_sm3_old)) {
							//数据一致不做操作
							map.put("code", "200");
							map.put("msg", "完整性验证成功");
							map.put("data", hmac_sm3);
							return map;
						}
						p.put("check_flag", "false");
						result = this.baseDao.update("yptmm.updateLogInfoById", p);
						map.put("code", "200");
						map.put("result", result);
						map.put("data", hmac_sm3);
						map.put("msg", "数据已被篡改");
					}
				}
			}else if("6".equals(type)) {
				dataList = this.baseDao.selectList("yptmm.queryJsQxInfo", p);
				if(dataList != null && !dataList.isEmpty()) {
					dataMap = dataList.get(0);
					String userid = String.valueOf(dataMap.getOrDefault("userid", ""));
					String roleid = String.valueOf(dataMap.getOrDefault("roleid", ""));
					String hmac_sm3_old = String.valueOf(dataMap.getOrDefault("hmac_sm3", ""));
					if("".equals(hmac_sm3_old)){
						map.put("code", "500");
						map.put("msg", "数据之前未做完整性");
						return map;
					}
					String resultStr = Stream.of(userid,roleid)
							.filter(s -> s != null && !s.isEmpty() && !"null".equals(s))
							.collect(Collectors.joining(""));
					if("".equals(resultStr)) {
						map.put("code", "500");
						map.put("msg", "需要完整性数据为空");
						return map;
					}
					String hmac_sm3 = SM3HMac(resultStr);
					if(!"".equals(hmac_sm3) && !"null".equals(hmac_sm3)) {
						if(hmac_sm3.equals(hmac_sm3_old)) {
							//数据一致不做操作
							map.put("code", "200");
							map.put("msg", "完整性验证成功");
							map.put("data", hmac_sm3);
							return map;
						}
						p.put("check_flag", "false");
						result = this.baseDao.update("yptmm.updateJsQxInfoById", p);
						map.put("code", "200");
						map.put("result", result);
						map.put("data", hmac_sm3);
						map.put("msg", "数据已被篡改");
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			log.info("数据操作异常："+ e.getMessage());
			map.put("code", "500");
			map.put("msg", "数据操作异常");
			return map;
		}
		return map;
	}


}
